#include <stdio.h>
#include <vector>
using namespace std;

bool IsReverse(vector<int> leftArr, int end, vector<int> rightArr, int start, int len)
{
    while (len > 0)
    {
        if (leftArr[end] != rightArr[start])
            return false;
        end--;
        start--;
        len--;
    }
    return true;
}

bool IsPopOrder(vector<int> pushV, vector<int> popV)
{
    if (pushV.size() != popV.size())
        return false;
    if (pushV.size() == 0)
        return true;

    int p = popV.size() - 1;
    for (int i = 0, len = pushV.size(); i < len;)
    {
        if (pushV[i] == popV[p])
        {
            i++;
            p--;
        }
        else
        {
            bool is_find = false;
            int j = 0;
            for (j = i + 1; j < len; ++j)
            {
                if (pushV[j] == popV[p])
                {
                    is_find = true;
                    break;
                }
            }
            if (!is_find)
                return false;
            int len = j - i + 1;
            bool result = IsReverse(pushV, j, popV, p, len);
            if (!result)
                return false;
            p = p - len;
            i = j + 1;
        }
    }
    return true;
}

int main()
{
    vector<int> push = {1,2,3,4,5};
    vector<int> pop = {4,5,3,2,1};
    printf("%d\n", IsPopOrder(push, pop));

    push = {1};
    pop = {2};
    printf("%d\n", IsPopOrder(push, pop));

    push = {1};
    pop = {1};
    printf("%d\n", IsPopOrder(push, pop));

    push = {};
    pop = {};
    printf("%d\n", IsPopOrder(push, pop));

    push = {};
    pop = {1};
    printf("%d\n", IsPopOrder(push, pop));
    return 0;
}